/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ht6_huffman;

import java.util.LinkedList;
import java.util.Queue;

/**
 * ArbolIterator
 * Basado en BTInorderIteratorR, Java Structures
 * @author Jorge Lainfiesta 11142
 * @since Sep 19, 2012
 * @version 1
 */
public class ArbolIterator {
    protected ArbolCaracter root;
    protected Queue<ArbolCaracter> todo;
    
    /**
     * Constructor de un iterador en orden
     * @param Arbol a iterar 
     */
    public ArbolIterator(ArbolCaracter root){
        todo = new LinkedList<ArbolCaracter>();
        this.root = root;
        this.reset();
    }
    /**
     * Resetea el iterador
     */
    public final void reset(){
        todo.clear();
        enqueueInorder(root);
    }
    /**
     * Encola los nodods hijos del actual arbol
     * @param current 
     */
    protected void enqueueInorder(ArbolBinario<Caracter> current){
        if(current == null)
            return;
        enqueueInorder(current.getLeft());
        if(current.getLeft()==null || current.getRight()==null)
            todo.offer((ArbolCaracter) current);
        enqueueInorder(current.getRight());
    }
    public ArbolCaracter next()
    // pre: hasNext();
    // post: returns current value, increments iterator
    {
        ArbolCaracter current = todo.poll();
        return (ArbolCaracter) current;
    }
    /**
     * Retorna el número de elementos 
     * @return 
     */
    public int size(){
        return todo.size();
    }
    /**
     * Revisa si quedan elementos
     * @return 
     */
    public boolean isEmpty(){
        return todo.isEmpty();
    }

}
